package com.jjwxc.jwjskandriod.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alipay.sdk.app.PayTask;
import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.config.BaseActivity;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.utils.StUtils;
import com.jjwxc.jwjskandriod.model.PayResult;
import com.jjwxc.jwjskandriod.model.RechargePriceResponse;
import com.jjwxc.jwjskandriod.model.RechargeResponse;
import com.jjwxc.jwjskandriod.model.UserBalanceResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 充值购买完结币
 */

public class RechargeCoinActivity extends BaseActivity {
    private TextView tv_balance_yue;
    private LinearLayout ll_coin_one;
    private LinearLayout ll_coin_two;
    private LinearLayout ll_coin_three;

    private TextView tv_wjb_one;
    private TextView tv_wjb_two;
    private TextView tv_wjb_three;

    private TextView tv_price_one;
    private TextView tv_price_two;
    private TextView tv_price_three;
    private TextView tv_yhxy;
    private TextView tv_recharge;
    private List<LinearLayout> llList = new ArrayList<>();
    private List<TextView> wjbList = new ArrayList<>();
    private List<TextView> priceList = new ArrayList<>();
    private int mBalance;
    private String selectId;

    @Override
    public int getContentViewId() {
        return R.layout.activity_rechargecoin;
    }

    @Override
    public void findView() {
        setNoTitle();
        tv_yhxy = findViewById(R.id.tv_yhxy);
        tv_balance_yue = findViewById(R.id.tv_balance_yue);
        ll_coin_one = findViewById(R.id.ll_coin_one);
        ll_coin_two = findViewById(R.id.ll_coin_two);
        ll_coin_three = findViewById(R.id.ll_coin_three);
        llList.add(ll_coin_one);
        llList.add(ll_coin_two);
        llList.add(ll_coin_three);
        tv_wjb_one = findViewById(R.id.tv_wjb_one);
        tv_wjb_two = findViewById(R.id.tv_wjb_two);
        tv_wjb_three = findViewById(R.id.tv_wjb_three);
        wjbList.add(tv_wjb_one);
        wjbList.add(tv_wjb_two);
        wjbList.add(tv_wjb_three);
        tv_price_one = findViewById(R.id.tv_price_one);
        tv_price_two = findViewById(R.id.tv_price_two);
        tv_price_three = findViewById(R.id.tv_price_three);
        priceList.add(tv_price_one);
        priceList.add(tv_price_two);
        priceList.add(tv_price_three);
        tv_recharge = findViewById(R.id.tv_recharge);

    }
    private static final int SDK_PAY_FLAG = 1;
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            e("***************");
            if (msg.what == SDK_PAY_FLAG) {
                PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                /**
                 * 对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                 */
                String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                String resultStatus = payResult.getResultStatus();
                // 判断resultStatus 为9000则代表支付成功
                if (TextUtils.equals(resultStatus, "9000")) {
                    // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                    e("-" + payResult);
                    showToast("支付完成");
                    //获取余额
                    getBalance();
                } else {
                    // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                    e("-" + payResult);
                }
            }
        }

        ;
    };

    @Override
    public void setListener() {
        tv_yhxy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(RechargeCoinActivity.this, WebActivity.class);
                intent.putExtra("WEB_URL", Url.active2);
                intent.putExtra("WEB_NAME", "用户服务协议");
                startActivity(intent);
            }
        });
        tv_recharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                post(Url.recharge, null, RechargeResponse.class, new FFNetWorkCallBack<RechargeResponse>() {
                    @Override
                    public void onSuccess(RechargeResponse response) {
                        if (response.getCode() == 200) {
                            Runnable payRunnable = new Runnable() {
                                @Override
                                public void run() {
                                    PayTask alipay = new PayTask(RechargeCoinActivity.this);
                                    Map <String,String> result = alipay.payV2(response.getData().getOrderStr(),true);
                                    Message msg = new Message();
                                    msg.what = SDK_PAY_FLAG;
                                    msg.obj = result;
                                    mHandler.sendMessage(msg);
                                }
                            };
                            // 必须异步调用
                            Thread payThread = new Thread(payRunnable);
                            payThread.start();

                        }

                    }

                    @Override
                    public boolean onFail(RechargeResponse response) {
                        return false;
                    }
                }, "{\"device\":\"android\",\"productId\":  \"" + selectId + "\"}");
            }
        });
    }

    @Override
    public void afterCreate() {
        //获取余额
        getBalance();

        //获取购买列表
        post(Url.getRechargePrice, "", RechargePriceResponse.class, new FFNetWorkCallBack<RechargePriceResponse>() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onSuccess(RechargePriceResponse response) {
                if (response.getCode() == 200 && response.getData().size() == 3) {
                    List<RechargePriceResponse.DataBean> beanList = response.getData();
                    for (int i = 0; i < llList.size(); i++) {
                        priceList.get(i).setText("￥" + StUtils.percentage(beanList.get(i).getPaymentAmount()));
                        wjbList.get(i).setText(beanList.get(i).getAmount() + "完结币");
                        if (i == 0) {
                            selectId = beanList.get(i).getProductId();
                            llList.get(i).setBackgroundResource(R.drawable.recharge_select_true);
                            priceList.get(i).setTextColor(Color.parseColor("#4D998D"));
                            wjbList.get(i).setTextColor(Color.parseColor("#4D998D"));
                        } else {
                            llList.get(i).setBackgroundResource(R.drawable.recharge_select_false);
                            priceList.get(i).setTextColor(Color.parseColor("#000000"));
                            wjbList.get(i).setTextColor(Color.parseColor("#000000"));
                        }
                        int finalI = i;
                        llList.get(i).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                for (int s = 0; s < llList.size(); s++) {
                                    if (finalI == s) {
                                        selectId = beanList.get(s).getProductId();
                                        llList.get(s).setBackgroundResource(R.drawable.recharge_select_true);
                                        priceList.get(s).setTextColor(Color.parseColor("#4D998D"));
                                        wjbList.get(s).setTextColor(Color.parseColor("#4D998D"));
                                    } else {
                                        llList.get(s).setBackgroundResource(R.drawable.recharge_select_false);
                                        priceList.get(s).setTextColor(Color.parseColor("#000000"));
                                        wjbList.get(s).setTextColor(Color.parseColor("#000000"));
                                    }
                                }
                            }
                        });
                    }

                }

            }

            @Override
            public boolean onFail(RechargePriceResponse response) {
                return false;
            }
        }, "{\"device\":\"android\"}");
    }

    public void getBalance() {
        post(Url.balance, null, UserBalanceResponse.class, new FFNetWorkCallBack<UserBalanceResponse>() {
            @Override
            public void onSuccess(UserBalanceResponse response) {
                if (response.getCode() == 200) {
                    mBalance = response.getData().getBalance();
                    tv_balance_yue.setText(mBalance + "完结币");
                }

            }

            @Override
            public boolean onFail(UserBalanceResponse response) {
                return false;
            }
        });
    }
}
